/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package demo.examples;

import com.sleekice.core.GameSpecs;
import com.sleekice.core.SleekCore;
import com.sleekice.input.InputAction;
import com.sleekice.input.KeyboardInput;
import com.sleekice.object.SimpleEntity;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

/**
 * A simple movement example showing SleekIce's input api.
 */
public class Movement extends SleekCore {
    
    private GameSpecs specs = new GameSpecs();
    
    private SimpleEntity ball = new SimpleEntity(100,100);
    
    /* keyboard input */
    private KeyboardInput kInput = new KeyboardInput();
    private InputAction moveUp = new InputAction();
    private InputAction moveLeft = new InputAction();
    private InputAction moveRight = new InputAction();
    private InputAction moveDown = new InputAction();
    private InputAction quitGame = new InputAction();
    
    public Movement() {
        kInput.addTrigger(moveUp , KeyEvent.VK_W);
        kInput.addTrigger(moveLeft , KeyEvent.VK_A);
        kInput.addTrigger(moveRight , KeyEvent.VK_D);
        kInput.addTrigger(moveDown , KeyEvent.VK_S);
        kInput.addTrigger(quitGame , KeyEvent.VK_ESCAPE);
        kInput.assignTo(this);
        
        specs.setScreenDimensions(400, 300);
        gameStart(specs);
    }

    public void onExit() {
        System.out.println("Game End");
    }

    public void update(double elapsedTime) {
        
        if (moveUp.isKeyPressed()) {
            ball.getVelocity().setSpeed(300);
            ball.getVelocity().setDirection(90);
        }
        if (moveLeft.isKeyPressed()) {
            ball.getVelocity().setSpeed(300);
            ball.getVelocity().setDirection(180);
        }
        if (moveRight.isKeyPressed()) {
            ball.getVelocity().setSpeed(300);
            ball.getVelocity().setDirection(0);
        }
        if (moveDown.isKeyPressed()) {
            ball.getVelocity().setSpeed(300);
            ball.getVelocity().setDirection(270);
        }
        if (quitGame.isKeyPressed()) {
            stop();
        }
        
        ball.update(elapsedTime);
        /* set speed to 0 to not move if wasd is not pressed next update
         * if wasd is pressed next update, the speed is set to 300 px/s
         */
        ball.getVelocity().setSpeed(0);
    }

    public void render(Graphics2D g) {
        /* clear the drawing surface */
        g.setColor(Color.black);
        g.fillRect(0,0,specs.getScreenWidth(),specs.getScreenHeight());
        
        /* draw the ball */
        ball.render(g);
        
        /* draw help */
        g.setColor(Color.white);
        g.drawString("Press the WASD keys to move, Esc to quit.", 10, 20);
    }
    
    public static void main(String[] args) {
        new Movement();
    }
}
